//@charset "utf-8";
// 问句 - 数据
define(function (require, exports, module) {
	var AjaxBaseModel = require('../models/ajaxbase');
	var clientQuote = require('../lib/clientquote');

	var QuerySearchModel = AjaxBaseModel.extend({
		url : 'http://zx.10jqka.com.cn/i/index/index/app/5/',
		getQueryResult : function (query, callback) {
			var self = this;
			self.set('result', '');

			self.callback = function () {
				self.fixQuoteData(callback);
			};
			self.request({
				w : query
			});
		},
		getQueryResultStr : function (tag) {
			var result = this.get('result');
			var codeList = [];
			$.each(result.data, function (i, item) {
				codeList.push(item['code']);
			});

			return codeList.join(tag);
		},
		fixQuoteData : function (callback) {
			var self = this;
			var result = self.get('result');
			if (undefined === result.data || result.data.length <= 0) {
				callback();
				return ;
			}
			var param = {
				code : result.data.join(','),
				type : 'zqmc,new,zhangdiefu'
			};
			var data = [];
			var quoteCallback = function (res) {
				for (var i in result.data) {
					var sortZhangdiefu = '--';
					if (
						undefined === res[result.data[i]]['zhangdiefu'] ||
						isNaN(res[result.data[i]]['zhangdiefu'])
					) {
						sortZhangdiefu = '--';
					} else {
						sortZhangdiefu = parseFloat(res[result.data[i]]['zhangdiefu']);
					}
					var newPrice = undefined !== res[result.data[i]]['new'] ? res[result.data[i]]['new'] : '--';
					var zhangdiefu = undefined !== res[result.data[i]]['zhangdiefu'] ? res[result.data[i]]['zhangdiefu'] : '--';
					data.push({
						'code' : result.data[i],
						'zqmc' : undefined !== res[result.data[i]]['zqmc'] ? 
							res[result.data[i]]['zqmc'] : '--',
						'newprice' : newPrice == '--' ? '--' : parseFloat(parseFloat(newPrice).toFixed(2)),
						'zhangdiefu' : zhangdiefu == '--' ? '--' : 	parseFloat(zhangdiefu).toFixed(2),
						'sortzdf' : sortZhangdiefu
					});
				}
				data = data.sort(self.sortTableFun('sortzdf', 'desc'));
				result.data = data;
				self.set('result', result);
				callback();
			};
			clientQuote.getStockMinData(param, quoteCallback);
		}
	});

	module.exports = new QuerySearchModel();
});
